package cn.ibizlab.codegen.groovy.support

import cn.ibizlab.codegen.groovy.util.JavaTypeUtil
import net.ibizsys.model.PSModelEnums.DEMethodReturnType
import net.ibizsys.model.dataentity.action.IPSDEActionReturn
import net.ibizsys.model.dataentity.ds.IPSDEDataSetReturn
import net.ibizsys.model.dataentity.service.IPSDEMethodReturn

public class PSDEMethodReturnExtension {

    public static String getJavaType(IPSDEMethodReturn iPSDEMethodReturn) {

        def returnType = iPSDEMethodReturn.getType()
        if (DEMethodReturnType.VOID.value == returnType) {
            return "void"
        }

        if (DEMethodReturnType.DTOS.value == returnType) {

            if (iPSDEMethodReturn instanceof IPSDEActionReturn) {
                IPSDEActionReturn iPSDEActionReturn = (IPSDEActionReturn) iPSDEMethodReturn
				return String.format("List<%s>", iPSDEActionReturn.getPSDEMethodDTOMust().getRealCodeName())
//                return "List<IEntity>"
            }

            if (iPSDEMethodReturn instanceof IPSDEDataSetReturn) {
                IPSDEDataSetReturn iPSDEDataSetReturn = (IPSDEDataSetReturn) iPSDEMethodReturn
				return String.format("List<%s>", iPSDEDataSetReturn.getPSDEMethodDTOMust().getRealCodeName())
//                return "List<IEntity>"
            }

            return "List"
        }

        if (DEMethodReturnType.PAGE.value == returnType) {
            if (iPSDEMethodReturn instanceof IPSDEDataSetReturn) {
                IPSDEDataSetReturn iPSDEDataSetReturn = (IPSDEDataSetReturn) iPSDEMethodReturn
				return String.format("Page<%s>", iPSDEDataSetReturn.getPSDEMethodDTOMust().getRealCodeName())
//                return "Page<IEntity>"
            }

            return "Page"
        }


        if (DEMethodReturnType.DTO.value == returnType) {

            if (iPSDEMethodReturn instanceof IPSDEActionReturn) {
                IPSDEActionReturn iPSDEActionReturn = (IPSDEActionReturn) iPSDEMethodReturn
                return iPSDEActionReturn.getPSDEMethodDTOMust().getRealCodeName()
//                return "IEntity"
            }

            if (iPSDEMethodReturn instanceof IPSDEDataSetReturn) {
                IPSDEDataSetReturn iPSDEDataSetReturn = (IPSDEDataSetReturn) iPSDEMethodReturn
                return iPSDEDataSetReturn.getPSDEMethodDTOMust().getRealCodeName()
//                return "IEntity"
            }

            return "Object"
        }

        if (DEMethodReturnType.SIMPLE.value == returnType || DEMethodReturnType.SIMPLES.value == returnType) {
            if (iPSDEMethodReturn instanceof IPSDEActionReturn) {
                IPSDEActionReturn iPSDEActionReturn = (IPSDEActionReturn) iPSDEMethodReturn
                String strJavaType = JavaTypeUtil.getJavaType(iPSDEActionReturn.getStdDataType())
                if (DEMethodReturnType.SIMPLES.value == returnType) {
                    if ("Object" == strJavaType) {
                        return "List"
                    }
                    return String.format("List<%s>", strJavaType)
                }

                return strJavaType
            }
            return "Object"
        }

        return "Object"
    }


}
